package starfield;
	
import java.util.ArrayList;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class MetadataVersion {
	private String stage;
	private ArrayList<Integer> numbers;

	public MetadataVersion(String stage, ArrayList<Integer> numbers) {
		this.stage = stage;
		this.numbers = numbers;
	}
	
	public static MetadataVersion parse(String s) throws ValidationException {
		return new MetadataVersion("PARSE", getNumbersFromString("0.1.2.3.4"));
	}
	
	@Override
	public String toString() {
		return stage + " " + getNumbersString();
	}
	
	public static ArrayList<Integer> getNumbersFromString(String s) throws ValidationException {
		String globalRegex = "((\\d+)\\.)*(\\d+)";
		String individualRegex = "(\\d+)";
		
		if (s.matches(globalRegex)) {
			Pattern pattern = Pattern.compile(individualRegex);
			Matcher matcher = pattern.matcher(s);
			
			ArrayList<Integer> numbers = new ArrayList<>();
			
			while (matcher.find()) {
				numbers.add(Integer.parseInt(matcher.group(1))); 
			}
			
			return numbers;
		} else {
			throw new ValidationException("Invalid version numbers.");
		}
	}
	
	public String getNumbersString() {
		if (getNumbers() == null) {
			return "";
		} else {
			StringBuilder sb = new StringBuilder();

			String delimiter = "";

			for (int number : getNumbers()) {
				sb.append(delimiter).append(number);
				delimiter = ".";
			}

			return sb.toString();
		}
	}

	public String getStage() {
		return stage;
	}

	public ArrayList<Integer> getNumbers() {
		return numbers;
	}
}